// https://www.acwing.com/problem/content/847/

#include <iostream>
#include <string>
#include <unordered_set>
#include <queue>
using namespace std;

queue<pair<int, int>> que1;    // 存储 x 的位置的队列
queue<string> que2;            // 存储对应字符串
unordered_set<string> history; // 用来记录历史中出现过的字符
string board = "123456789";    // 接收输入的字符串
const string target = "12345678x";   // 这个是目标
int to[4][2] = {{0, -1}, {1, 0}, {-1, 0}, {0, 1}};

int calcIndex(int x, int y) {
    return x * 3 + y;
}

int bfs() {
    int steps = 0;
    while (!que1.empty()) {
        int size = que1.size();
        for (int i=0; i<size; i++) {
            auto [x, y] = que1.front();
            auto word = que2.front();
            que1.pop(); que2.pop();
            for (auto & t: to) {
                int tx = x + t[0], ty = y + t[1];
                if (tx >= 0 && ty >= 0 && tx < 3 && ty < 3) {
                    int idx1 = calcIndex(x, y), idx2 = calcIndex(tx, ty);
                    swap(word[idx1], word[idx2]);
                    if (history.find(target) != history.end()) {
                        return steps + 1;
                    }

                    if (history.find(word) == history.end()) {
                        que1.push({tx, ty});
                        que2.push(word);
                        history.insert(word);
                    }
                    swap(word[idx1], word[idx2]);
                }
            }
        }
        steps++;
    }
    return -1;
}

/*
1 2 3 x 4 6 7 5 8
[3]

1 2 3 4 5 6 7 8 x
[0]

2 3 4 1 5 x 7 6 8
[19]
*/
int main() {
    char ch;
    for (int i=0; i<9; i++) {
        cin>>ch;
        board[i] = ch;
    }
    history.insert(board);
    if (history.find(target) != history.end()) {
        cout<<"0"<<endl;
        return 0;
    }
    int x, y;
    for (int i=0; i<3; i++) {
        for (int j=0; j<3; j++) {
            if (board[i * 3 + j] == 'x') {
                x = i; y = j;
                break;
            }
        }
    }
    que1.push({x, y});
    que2.push(board);
    cout<<bfs()<<endl;
    return 0;
}
